<script src="__PUBLIC_HIS__/vendor/layer/layer.js"></script>
<div style=" width: 560px; min-height: 360px; padding: 10px; margin: auto; margin-top:10px; background: #FFFFFF;">

    <ul class="tabBtn clearfix headingTab bcb">
        <li class="on">退款</li>
        <li>支付记录</li>
        <li>已退款记录</li>
    </ul>
    <ul class="tabBox" style="max-height:846px; overflow-y: auto;">
        <li class="on" id="care_history_box">
            <ul class="mall_list">
                <foreach name="mall_list" key="k" item="v">

                    <li>
                        <if condition="$v[status] eq 1">
                            <label><input class="item" type="checkbox" value="{$v.id}"
                                          data-all_amount="{$v.all_amount}"> {$v.label}</label> <span class="r">{$v.all_amount}元</span>

                            <else/>

                            <label>&nbsp;&nbsp; {$v.label}</label> <span class="r"
                                                                         style="color: gray;">{$v.all_amount}元</span>

                        </if>
                    </li>
                </foreach>
            </ul>

            <div style="padding: 10px; border-top:#000000 1px solid;margin-top: 10px;">
                退款金额：<span class="r" id="refund_amount">￥0.00</span>
            </div>

            <div style="padding: 15px;">
                <label><input type="checkbox" id="chk_all">全选</label>
                <label><input type="checkbox" id="chk_all2">反选</label>
                <span class="r">
                    <label title="整单退，一次全部退款"><input type="radio" value="0" name="refund_type" id="refund_type_0"
                                                     onclick="change_refund_type(0);"/> 原路退回(整单)</label>
                    <label title="部分退款，可多次退款"><input type="radio" value="1" name="refund_type" id="refund_type_1"
                                                     checked onclick="change_refund_type(1);"/> 使用现金退回（部分）</label>
                </span>
            </div>

            <div id="cash_box" style="display: ;padding: 8px;">
                金额：<input type="number" id="cash" value="" placeholder="" max="1000000" maxlength="10"/>
                <small style="color: gray;">现金退款金额</small>
            </div>

            <div style="text-align: center;">
                <textarea placeholder="退款说明" id="memo" cols="60" rows="3"></textarea>
            </div>
            <div style="padding: 15px;">

                <button type="button" class="btn btn-primary wb100" id="btn_refund">退款</button>
            </div>

        </li>
        <li>
            <ul class="mall_list">
                <foreach name="list_pay" key="k" item="v">
                    <li>
                        {$v.payment_platform_label}
                        <span class="r">￥{$v.pay_amount}</span>
                    </li>
                </foreach>
            </ul>

        </li>
        <li>
            <ul class="mall_list" id="refund_list_box">
                <li>无记录</li>
            </ul>

        </li>
    </ul>

</div>


<style>

    #refund_amount {
        color: #FF0000;
        font-weight: bold;
        font-size: large;
        margin-right: 10px;
    }

    .mall_list {
        min-height: 200px;
        padding: 10px;
        max-height: 200px;
        overflow-y: scroll;
    }

    .mall_list li {
        border-bottom: #F0F0F0 1px solid;
        line-height: 35px;
        height: 35px;
    }

    .tr_registration_item, .tr_patient_list_item, .tr_medicines_list_item, .tr_inspection_list_item, .tr_extracharges_list_item, .history_list_item {
        cursor: pointer;
    }

    #registration_pager_box, #patient_list_pager_box {
        text-align: center;
        width: 100%;
    }

    #registration_box .on, #patient_list_box .on, #medicines_list_box .on, #inspectionfee_list_box .on, #extracharges_list_box .on, #history_search_list_box .on {
        background: #1a8cff;
        color: #FFFFFF;
    }

    #medicines_list_box .sel, #inspectionfee_list_box .sel, #extracharges_list_box .sel {
        background: #0CA818;
        color: #FFFFFF;
    }

    .care_order_sub_box {
        border: 1px solid #0E3460;
        min-height: 50px;
        padding: 12px;
        margin-bottom: 12px;
    }

    .care_order_sub_box span {
        display: inline-block;
        padding: 5px;
        margin-right: 2px;
    }

    .medicines_show {
        display: none;
    }

    .pay_status {
        padding: 6px 8px;
    }

    .unpay {
        background: #7C7C7C;
        color: #FF0000;
    }

    .payed {
        background: #0CA818;
        color: #FFFFFF;
    }

    .form-group {
        line-height: 35px;
        height: 35px;
    }

    #pay_tab td {
        border: none;
    }
</style>
<!-- END WRAPPER -->
<script>
    var is_iframe = self != top ? true : false;
    var pkg_amount = {$pkg.amount
    }
    ;
    var list_pay = {$list_pay_json};
    var ids = '';

    if (is_iframe) {
        if (parent.my_layer) {
            my_layer = parent.my_layer;
        } else {
            my_layer = parent.layer;
        }
    } else {
        my_layer = layer;
    }


    function change_refund_type(tid) {
        if (tid == 0) {
            $("#cash_box").hide();
        } else {
            $("#cash_box").show();
        }

        count_amount();
    }

    $(function () {


        //实现全选反选
        $("#chk_all").on('click', function () {

            $(".item").prop("checked", $(this).prop('checked'));
            count_amount(0);
        });

        $("#chk_all2").on('click', function () {

            $(".item").each(function (i, n) {


                if ($(this).prop("checked")) {
                    $(this).prop("checked", false);
                }
                else {
                    $(this).prop("checked", true);
                }
            });
            count_amount(0);
        });

        $(".item").click(function () {
            count_amount(0);
        });

        $("#cash").on('input propertychange', function () {
            count_amount(1);
        });


        $(document).on('click', '.tabBtn > li', function () {
            $(this).addClass('on').siblings('li').removeClass('on').closest('.tabBtn');
            $(this).closest('.tabBtn').siblings('.tabBox').find('> li').eq($(this).index()).addClass('on').siblings('li').removeClass('on');

            /*var t = $(this).html();
            if(t=='已退款记录'){
                getRefundLog();
            }*/

        });

        $("#btn_refund").click(function () {
            refund_do();
        });


        getRefundLog();
    });

    function refund_do() {
        //parent.layer_loading('退款中');
        var memo = $("#memo").val();
        var cash = $("#cash").val();
        var refund_type = $("#refund_type_1").prop('checked') ? 1 : 0;
        if (memo.length < 2) {
            my_layer.msg('退款说明不能为空！');
            $("#memo").focus();
            return false;
        }

        if (refund_type == 1) {
            if (cash.length == 0 || cash <= 0) {
                my_layer.msg('退款金额应大于0！');
                $("#cash").focus();
                return false;
            }
        }


        $.post('/Doctor/pkgRefundDo', {
            pkg_id: '{$pkg.id}',
            ids: ids,
            refund_type: refund_type,
            cash: cash,
            memo: memo
        }, function (res) {
            //parent.layer_close_loading();
            if (res.status == 0) {
                //window.location.reload();
                /*$("#memo").val('');
                $("#case").val(0);

                my_layer.msg('操作成功');*/
                if (parent.refund_back) {
                    parent.refund_back('{$pkg.id}', res.data.status);
                } else {
                    refund_back('{$pkg.id}', res.data.status);
                }

                //getRefundLog();
            } else {
                alert(res.msg);
            }
        });
    }


    function count_amount(type) {

        var rm = 0, ts = '';


        $(".item").each(function (i, n) {
            if ($(n).prop('checked')) {
                rm += parseFloat($(n).data('all_amount'));
                ts += ts == '' ? $(n).val() : ',' + $(n).val();
            }
        });


        if (type == 1) {
            var val = $("#cash").val();
            if (val.length == 0) val = 0;
            rm = parseFloat(val);
            if (rm > 10000) {
                my_layer.msg('注意：超过1万了')
            }

            if (rm > 100000) {
                my_layer.msg('注意：超过10万了')
            }

            if (rm > 1000000) {
                my_layer.msg('注意：操作最大上限不能超过100万');
                rm = 1000000;
                $("#cash").val(rm.toFixed(2));
            }

        } else {

            $("#cash").val(rm.toFixed(2));
        }

        var is_full = false;
        if (rm > 0) {
            is_full = rm == pkg_amount;
            $("#btn_refund").prop('disabled', false);

        } else {
            //$("#btn_refund").prop('disabled',true);

        }

        /*if(is_full){
            $("#refund_type_0").prop('disabled',false);
            //$("#refund_type_1").prop('checked',false);
        }else{
            $("#refund_type_0").prop('checked',false).prop('disabled',true);
            //$("#refund_type_0")
            $("#refund_type_1").prop('checked',true);
            $("#cash_box").show();
        }*/


        ids = ts;


        $("#refund_amount").html('￥' + rm.toFixed(2));
    }


    function getRefundLog() {
        $.get('/Doctor/getRefundLog/pkg_id/{$pkg.id}', function (res) {
            if (res.status == 0) {
                if (res.data.num > 0) {
                    var h = '';
                    $.each(res.data.list, function (i, n) {

                        h += '<li title="' + n.adm_memo + '">\n' +
                            n.payment_platform_label +
                            '<span style="margin-left: 10px;">' + n.addtime + '</span>' +
                            '<span class="r">￥' + n.refund_amount + '</span>\n' +
                            '</li>';

                    });
                    $("#refund_list_box").html(h);
                }
            } else {
                alert(res.msg);
            }
        });
    }
</script>
